<?php

class check_date
{

    private static $_holiday = array();
    
    private static $_options = array(
        'excluded_saturday' => true,
        'excluded_sunday' => true,
        'excluded_holiday' => true,
    );

    public static function set_option($option = array()){
        array_merge(self::$_options, $option);
    }

    public static function set_holidays($holidays = array())
    {
        self::$_holiday = $holidays;
    }

    public static function check_time_status($status_time, $status_time_exist)
    {
        return self::check_time($status_time, date('Y-m-d H:i:s'), $status_time_exist);
    }

    public static function check_time($from_date, $to_date, $time_status_exist)
    {
        $holidays_in_range = self::_get_holiday_in_range($from_date, $to_date, self::$_holiday);
        if (self::_check_holiday($from_date, self::$_holiday))
        {
            $from_date = date('Y-m-d 00:00:00', strtotime('+1 day', strtotime($from_date)));
        }
        if (self::_check_holiday($to_date, self::$_holiday))
        {
            $to_date = date('Y-m-d 23:59:59', strtotime('-1 day', strtotime($to_date)));
        }

        $diff = abs(strtotime($from_date) - strtotime($to_date));
        $diff_hour = ceil($diff / (60 * 60));

        return $diff_hour - $holidays_in_range * 24 > $time_status_exist ? TRUE : FALSE;
    }

    private static function _get_holiday_in_range($from_date, $to_date, $holidays)
    {
        $from = strtotime('+1 days', strtotime($from_date));
        $to = strtotime($to_date);
        $holiday_in_range = 0;

        while ($from < $to) {
            $date = date('Y-m-d H:i:s', $from);
            if (self::_check_holiday($date, $holidays) === TRUE)
            {
                $holiday_in_range++;
            }
            $from = strtotime('+1 day', $from);
        }
        return $holiday_in_range;
    }

    private static function _check_holiday($date_check, $holidays)
    {
        $time = strtotime($date_check);
        $result = (self::$_options['excluded_holiday'] && in_array(date('Y-m-d', $time), $holidays)) || 
        		(self::$_options['excluded_saturday'] && date('l', $time) === 'Saturday')  || 
        		(self::$_options['excluded_sunday'] && date('l', $time) === 'Sunday');
        
        return $result;
    }

}

?>
